//
//  SRSilenceClockTableViewCell.m
//  SportRing
//
//  Created by elwin on 11/4/14.
//  Copyright (c) 2014 HotOne. All rights reserved.
//

#import "SRSilenceClockTableViewCell.h"
#import "UIColor+LG.h"
#import "SRMe.h"
#import "SRMe+Cate.h"

@implementation SRSilenceClockTableViewCellModel : NSObject

- (id)copyWithZone:(NSZone *)zone
{
    SRSilenceClockTableViewCellModel* model = [[SRSilenceClockTableViewCellModel allocWithZone:zone] init];
    
    [model setTime:self.time];
    [model setMonday:self.monday];
    [model setTuesday:self.tuesday];
    [model setWednesday:self.wednesday];
    [model setThursday:self.thursday];
    [model setFriday:self.friday];
    [model setSaturday:self.saturday];
    [model setSunday:self.sunday];
    [model setOpenSwitch:self.openSwitch];
    [model setRingType:self.ringType];
    [model setAlarmId:self.alarmId];
    
    return model;
}

+ (SRAlarm *)toSRAlarm:(SRSilenceClockTableViewCellModel *)model
{
    if (!model) {
        NSLog(@"闹钟为空");
    }
    SRAlarm * alarm = [SRAlarm findAlarmForMe:[SRMe currentUser]
                                      alarmId:[model.alarmId integerValue]];
    
    [alarm configAttributesWithBlock:^(SRAlarm *aSelf) {
        NSArray * comps = [model.time componentsSeparatedByString:@":"];
        int time = [comps[0] intValue] * 60 + [comps[1] intValue];
        aSelf.time = @(time);
        
        aSelf.on = @(model.openSwitch);
        aSelf.alarmId = model.alarmId;
        aSelf.me = [SRMe currentUser];
        if (model.ringType == CLOCK_RING_TYPE_ONE_TIME)
        {
            aSelf.once = @(YES);
        }
        else
        {
            aSelf.once = @(NO);
            
        }
        
        if (model.monday.boolValue)
        {
            aSelf.monday = @(YES);
        }
        else
        {
            aSelf.monday = @(NO);

        }
        if (model.tuesday.boolValue)
        {
            aSelf.tuesday = @(YES);
        }
        else
        {
            aSelf.tuesday = @(NO);
            
        }
        
        if (model.wednesday.boolValue)
        {
            aSelf.wednesday = @(YES);
        }
        else
        {
            aSelf.wednesday = @(NO);
            
        }
        
        if (model.thursday.boolValue)
        {
            aSelf.thursday = @(YES);
        }
        else
        {
            aSelf.thursday = @(NO);
            
        }
        
        if (model.friday.boolValue)
        {
            aSelf.friday = @(YES);
        }
        else
        {
            aSelf.friday = @(NO);
            
        }
        
        if (model.saturday.boolValue)
        {
            aSelf.saturday = @(YES);
        }
        
        else
        {
            aSelf.saturday = @(NO);
            
        }
        
        if (model.sunday.boolValue)
        {
            aSelf.sunday = @(YES);
        }
        else
        {
            aSelf.sunday = @(NO);
        }
        
        
    }];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateTableview" object:nil];
    return alarm;
}

+ (NSMutableArray *)selectedWeekDays:(SRSilenceClockTableViewCellModel *)model
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    if (model.monday.boolValue)
    {
        [arr addObject:@1];
    }

    if (model.tuesday.boolValue)
    {
        [arr addObject:@2];
    }

    if (model.wednesday.boolValue)
    {
        [arr addObject:@3];
    }
    
    if (model.thursday.boolValue)
    {
        [arr addObject:@4];
    }

    if (model.friday.boolValue)
    {
        [arr addObject:@5];
    }

    if (model.saturday.boolValue)
    {
        [arr addObject:@6];
    }

    if (model.sunday.boolValue)
    {
        [arr addObject:@0];
    }
    
    return arr;
}




@end

@interface SRSilenceClockTableViewCell()

@property (nonatomic, weak)IBOutlet UILabel *scTimeLabel;
@property (nonatomic, weak)IBOutlet UILabel *scCustomWeekDayLabel;
@property (nonatomic, weak)IBOutlet UISwitch *openClockSwitch;

- (void)setTime:(NSString *)time;
- (void)setOpenSwitch:(BOOL)open;

@end

@implementation SRSilenceClockTableViewCell

- (void)setSRSilenceClockTableViewCellData:(SRSilenceClockTableViewCellModel *)model
{
    [self setTime:model.time];
    [self setOpenSwitch:model.openSwitch];
    [self setCustomWeekDay:model openSwitch:self.openClockSwitch.on];
}

- (void)setTime:(NSString *)time;
{
    self.scTimeLabel.text = time;
}

- (void)setCustomWeekDay:(SRSilenceClockTableViewCellModel *)model openSwitch:(BOOL)open
{
    if (open)
    {
        NSMutableString *weekDays = [[NSMutableString alloc] init];
        if (model.sunday.boolValue)
        {
            [weekDays appendString:@"日 "];
        }
        if (model.monday.boolValue)
        {
            [weekDays appendString:@"一 "];
        }
        if (model.tuesday.boolValue)
        {
            [weekDays appendString:@"二 "];
        }
        if (model.wednesday.boolValue)
        {
            [weekDays appendString:@"三 "];
        }
        if (model.thursday.boolValue)
        {
            [weekDays appendString:@"四 "];
        }
        if (model.friday.boolValue)
        {
            [weekDays appendString:@"五 "];
        }
        if (model.saturday.boolValue)
        {
            [weekDays appendString:@"六 "];
        }
        
        
        if (model.ringType == CLOCK_RING_TYPE_ONE_TIME)
        {
            [weekDays appendString:@"只响一次 "];
         }
        
        self.scCustomWeekDayLabel.text = weekDays;
        self.scCustomWeekDayLabel.textColor = [UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1];
        self.scTimeLabel.textColor = [UIColor darkTextColor];
    }
    else
    {
        self.scCustomWeekDayLabel.text = @"未开启";
        self.scCustomWeekDayLabel.textColor = [UIColor lightGrayColor];
        self.scTimeLabel.textColor = [UIColor lightGrayColor];
    }
}

- (void)setOpenSwitch:(BOOL)open
{
    self.openClockSwitch.on = open;
}

- (IBAction)switchAction:(id)sender
{
    UISwitch *open = (UISwitch *)sender;

    if (_srSilenceDelegate && [_srSilenceDelegate respondsToSelector:@selector(switchOn:cell:)])
    {
        [_srSilenceDelegate switchOn:open.on cell:self];
    }
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
